class Solution
{
public:
    int magicTower(vector<int> &nums)
    {
        priority_queue<int> pq;
        long long posSum = 1;
        long long negSum = 0;
        long long curNegSum = 0;
        int result = 0;

        for (auto num : nums)
        {
            if (num >= 0)
            {
                posSum += num;
            }
            else
            {
                negSum += -num;
                curNegSum += -num;
                pq.push(-num);
            }
            while (curNegSum >= posSum)
            {
                if (pq.empty())
                {
                    return -1;
                }
                curNegSum -= pq.top();
                pq.pop();
                ++result;
            }
        }
        if (negSum >= posSum)
        {
            return -1;
        }
        return result;
    }
};